/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BussinessObject.DanhmucBO;
import BussinessObject.GeneralBO;
import BussinessObject.SanphamBO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ngoctoan
 */
public class SanphamGUI extends javax.swing.JInternalFrame {

    private DanhmucBO categories = new DanhmucBO();
    private SanphamBO products = new SanphamBO();
    private DefaultTableModel model = new DefaultTableModel();
    private Locale locale = new Locale("vi", "VN");
    private NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

    /**
     * Creates new form SanphamGUI
     */
    public SanphamGUI() {
        initComponents();

        model.setColumnIdentifiers(new String[]{"Mã sp", "Tên sp", "Danh mục", "Giá bán", "Số lượng", "ĐVT", "Xuất xứ", "Mô tả"});
        tableProduct.setModel(model);

        this.GetCategories();
        this.GetProduct();

        txtID.setText("0");
        txtID.setVisible(false);
        TaoMasp();
    }

    private void TaoMasp() {
        try {
            ResultSet rs = products.LayIDsp();
            int returnId = 0;

            while (rs.next()) {
                returnId = Integer.parseInt(rs.getString(1)) + 1;
                txtMasp.setText("SP" + String.format("%05d", returnId));
            }
        } catch (SQLException ex) {
            Logger.getLogger(SanphamGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void GetProduct() {
        try {
            ResultSet result = products.GetAll();
            ResultSet rs = products.GetAll();
            model.setRowCount(GeneralBO.getNumRowRS(result));

            int row = 0;
            while (rs.next()) {
                try {
                    model.setValueAt(rs.getString("Masp"), row, 0);
                    model.setValueAt(rs.getString("Tensp"), row, 1);
                    model.setValueAt(rs.getString("Tendm"), row, 2);
                    model.setValueAt(fmt.format(Double.parseDouble(rs.getString("Giaban"))), row, 3);
                    model.setValueAt(rs.getString("Soluong"), row, 4);
                    model.setValueAt(rs.getString("Donvitinh"), row, 5);
                    model.setValueAt(rs.getString("Xuatxu"), row, 6);
                    model.setValueAt(rs.getString("Mota"), row, 7);

                    row++;
                } catch (SQLException ex) {
                    Logger.getLogger(SanphamGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            tableProduct.setModel(model);
            tableProduct.changeSelection(0, 0, false, false);
        } catch (SQLException ex) {
            Logger.getLogger(SanphamGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Load data combobox categories
     */
    public void GetCategories() {
        try {
            ResultSet result = categories.LayTatCaDuLieu();
            GeneralBO.HienRSLenCombobox(result, comboCategories);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtMota = new javax.swing.JTextArea();
        BtnContinue = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        BtnDelete = new javax.swing.JButton();
        BtnExit = new javax.swing.JButton();
        txtMasp = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        comboCategories = new javax.swing.JComboBox();
        txtGiaban = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtTensp = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtDvt = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        txtID = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtXuatxu = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableProduct = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        BtnFinder = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        txtKeyWord = new javax.swing.JTextField();

        setClosable(true);
        setTitle("Quản lý sản phẩm");
        setPreferredSize(new java.awt.Dimension(900, 637));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Mã sản phẩm");

        txtMota.setColumns(20);
        txtMota.setRows(5);
        jScrollPane2.setViewportView(txtMota);

        BtnContinue.setText("Nhập tiếp");
        BtnContinue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnContinueActionPerformed(evt);
            }
        });

        btnSave.setText("Lưu");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        BtnDelete.setText("Xóa");
        BtnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnDeleteActionPerformed(evt);
            }
        });

        BtnExit.setText("Thoát");
        BtnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnExitActionPerformed(evt);
            }
        });

        txtMasp.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Danh mục");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Mô tả");

        comboCategories.setName("comboCategories"); // NOI18N
        comboCategories.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCategoriesActionPerformed(evt);
            }
        });

        txtGiaban.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Tên sản phẩm");

        txtTensp.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setText("Đơn vị tính");

        txtDvt.setPreferredSize(new java.awt.Dimension(10, 25));
        txtDvt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDvtKeyPressed(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setText("Giá bán");

        jButton7.setPreferredSize(new java.awt.Dimension(40, 40));

        txtID.setText("jTextField1");

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setText("Xuất xứ");

        txtXuatxu.setPreferredSize(new java.awt.Dimension(10, 25));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel5)
                    .addComponent(jLabel10)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(43, 43, 43)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(comboCategories, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtGiaban, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtTensp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtMasp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtDvt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(106, 106, 106)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtnContinue, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                        .addComponent(txtXuatxu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(28, 28, 28))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtMasp, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(60, 60, 60)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(txtXuatxu, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(53, 53, 53)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtTensp, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(8, 8, 8)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(comboCategories, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtDvt, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtGiaban, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BtnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BtnContinue, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BtnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(txtID))
                .addContainerGap())
        );

        tableProduct.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tableProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableProduct.setRowHeight(25);
        tableProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableProductMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableProduct);

        BtnFinder.setText("Tìm kiếm");
        BtnFinder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnFinderActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Từ khóa");

        txtKeyWord.setPreferredSize(new java.awt.Dimension(10, 25));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtKeyWord, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BtnFinder, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnFinder, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtKeyWord, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboCategoriesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCategoriesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboCategoriesActionPerformed

    /*
     * Event click for Button save
     */
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try {
            String ID_Sanpham = txtID.getText();
            String Masp = txtMasp.getText().trim();
            String Tensp = txtTensp.getText().trim();
            int Madm = Integer.parseInt(comboCategories.getSelectedItem().toString());

            double Giaban = 0;
            int Soluong = 0;
            String Donvitinh = txtDvt.getText().trim();
            String Xuatxu = txtGiaban.getText().trim();
            String Mota = txtMota.getText().trim();
            //jErrorProvider.clear();
            if ("0".equals(ID_Sanpham)) {
                products.Insert(Masp, Tensp, Madm, Giaban, Soluong, Donvitinh, Xuatxu, Mota);
                TaoMasp();
            } else {
                products.Update(ID_Sanpham, Masp, Tensp, Madm, Giaban, Soluong, Donvitinh, Xuatxu, Mota);
            }
            this.GetProduct();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    /*
     * Event selected row to edit product
     */
    private void tableProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableProductMouseClicked
        try {
            txtID.setText(tableProduct.getValueAt(tableProduct.getSelectedRow(), 0).toString());
            txtMasp.setText(tableProduct.getValueAt(tableProduct.getSelectedRow(), 1).toString());
            txtTensp.setText(tableProduct.getValueAt(tableProduct.getSelectedColumn(), 2).toString());
            int index = (int) tableProduct.getValueAt(tableProduct.getSelectedRow(), 3);
            if (index != 1) {
                //JOptionPane.showMessageDialog(null,index,"Xóa danh mục",JOptionPane.NO_OPTION);
                comboCategories.setSelectedIndex(index - 2);
            }
            txtDvt.setText(tableProduct.getValueAt(tableProduct.getSelectedColumn(), 6).toString());
            txtGiaban.setText(tableProduct.getValueAt(tableProduct.getSelectedColumn(), 7).toString());
            txtMota.setText(tableProduct.getValueAt(tableProduct.getSelectedColumn(), 8).toString());
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }//GEN-LAST:event_tableProductMouseClicked

    /*
     * Event click for delete product
     */
    private void BtnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnDeleteActionPerformed
        String ID_Sanpham = txtID.getText();
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int dialogResult = JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn xóa sản phẩm không?", "Xóa sản phẩm", dialogButton);
        if (dialogResult == 0) {
            products.Delete(ID_Sanpham);
            this.GetProduct();
            JOptionPane.showMessageDialog(null, "Xóa thành công sản phẩm!", "Xóa sản phẩm", JOptionPane.NO_OPTION);
        }
    }//GEN-LAST:event_BtnDeleteActionPerformed

    /*
     * Finder products
     */
    private void BtnFinderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFinderActionPerformed
        // TODO add your handling code here:
        String keyWord = txtKeyWord.getText().trim();
        tableProduct.removeAll();
        try {
            ResultSet result = products.Find(keyWord);
            GeneralBO.HienRSLenTB(result, tableProduct);
        } catch (Exception e) {
            System.out.println(e);
        }
    }//GEN-LAST:event_BtnFinderActionPerformed

    /*
     * Exit application
     */
    private void BtnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_BtnExitActionPerformed

    /*
     * Reset Form
     */
    private void BtnContinueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnContinueActionPerformed
        txtID.setText("0");
        txtMasp.setText("");
        txtTensp.setText("");
        int index = (int) tableProduct.getValueAt(tableProduct.getSelectedRow(), 3);
        comboCategories.setSelectedIndex(0);
        txtDvt.setText("0");
        txtGiaban.setText("0");
        txtGiaban.setText("");
        txtMota.setText("");
    }//GEN-LAST:event_BtnContinueActionPerformed

    private void txtDvtKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDvtKeyPressed
        char c = evt.getKeyChar();
        if (!((c >= '0') && (c <= '9') || (c == evt.VK_BACK_SPACE) || (c == evt.VK_DELETE) || (c == evt.VK_ENTER) || (c == evt.VK_PERIOD))) {
            evt.consume();
        }
    }//GEN-LAST:event_txtDvtKeyPressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnContinue;
    private javax.swing.JButton BtnDelete;
    private javax.swing.JButton BtnExit;
    private javax.swing.JButton BtnFinder;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox comboCategories;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tableProduct;
    private javax.swing.JTextField txtDvt;
    private javax.swing.JTextField txtGiaban;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtKeyWord;
    private javax.swing.JTextField txtMasp;
    private javax.swing.JTextArea txtMota;
    private javax.swing.JTextField txtTensp;
    private javax.swing.JTextField txtXuatxu;
    // End of variables declaration//GEN-END:variables
}
